package com.hg.library.ui.gallery;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;

import com.hg.library.graph.ImageGetHelper;
import com.hg.library.graph.ImageProcess;
import com.hg.library.utils.AnimUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by wangjianqiang on 2016/4/22.
 */
public class CategoryGallery extends Gallery {
    private View root = null;
    private View currentView;

    public CategoryGallery(Context context) {
        super(context);
        init();
    }

    public CategoryGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CategoryGallery(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnItemSelectedListener(new MyOnItemSelectedListener());
    }

    public void setRootView(View root) {
        this.root = root;
    }

    class MyOnItemSelectedListener implements OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            int resoureID = (int) getAdapter().getItem(position);
            if (root != null) {
                ImageGetHelper.setBackGroundDrawable(root,getContext(),resoureID);
            }
            if (hasFocus()) {
                if (currentView != null) {
                    AnimUtils.scaleAnim(currentView, 500, 1.3f, 1.0f);
                }
                AnimUtils.scaleAnim(view, 500, 1.0f, 1.3f);
            }
            currentView = view;
            if(view != null){
                view.setAlpha(1);
                handleAlpha(view);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    }
    public void scaleBigger(){
        if(currentView == null){
            AnimUtils.scaleAnim(getSelectedView(), 500, 1.0f, 1.3f);
        }else{
            AnimUtils.scaleAnim(currentView, 500, 1.0f, 1.3f);
        }
    }
    public void scalesmaller(){
        if(currentView == null){
            AnimUtils.scaleAnim(getSelectedView(), 500, 1.3f, 1.0f);
        }else{
            AnimUtils.scaleAnim(currentView, 500, 1.3f, 1.0f);
        }
    }
    private void handleAlpha(View view){
        int position = -1;
        for(int i = 0; i < getChildCount(); i++){
            if(view == getChildAt(i)){
                position = i;
            }
        }
        View view0,view1,view2,view3,view4,view5,view6,view7;
        switch (position){
            case 0:
                break;
            case 1:
                 view0 = getChildAt(0);
                 view2 = getChildAt(2);
                if(view0 != null){
                    view0.setAlpha(0.6f);
                }
                if(view2 != null){
                    view2.setAlpha(0.6f);
                }
                break;
            case 2:
                view0 = getChildAt(0);
                view1 = getChildAt(1);
                if(view0 != null){
                    view0.setAlpha(0.4f);
                }
                if(view1 != null){
                    view1.setAlpha(0.6f);
                }
                view3 = getChildAt(3);
                view4 = getChildAt(4);
                view5 = getChildAt(5);
                view6 = getChildAt(6);
                if(view3 != null){
                    view3.setAlpha(0.6f);
                }
                if(view4 != null){
                    view4.setAlpha(0.4f);
                }
                if(view5 != null){
                    view5.setAlpha(0.2f);
                }
                if(view6 != null){
                    view6.setAlpha(0.2f);
                }
                break;
            case 3:
                view0 = getChildAt(0);
                view1 = getChildAt(1);
                view2 = getChildAt(2);
                if(view0 != null){
                    view0.setAlpha(0.2f);
                }
                if(view1 != null){
                    view1.setAlpha(0.4f);
                }
                if(view2 != null){
                    view2.setAlpha(0.6f);
                }
                view4 = getChildAt(4);
                view5 = getChildAt(5);
                view6 = getChildAt(6);
                view7 = getChildAt(7);
                if(view4 != null){
                    view4.setAlpha(0.6f);
                }
                if(view5 != null){
                    view5.setAlpha(0.4f);
                }
                if(view6 != null){
                    view6.setAlpha(0.2f);
                }
                if(view7 != null){
                    view7.setAlpha(0.2f);
                }
                break;
            case 4:
                view0 = getChildAt(0);
                view1 = getChildAt(1);
                view2 = getChildAt(2);
                view3 = getChildAt(3);
                if(view0 != null){
                    view0.setAlpha(0.2f);
                }
                if(view1 != null){
                    view1.setAlpha(0.2f);
                }
                if(view2 != null){
                    view2.setAlpha(0.4f);
                }
                if(view3 != null){
                    view3.setAlpha(0.6f);
                }
                view5 = getChildAt(5);
                view6 = getChildAt(6);
                view7 = getChildAt(7);
                if(view5 != null){
                    view5.setAlpha(0.6f);
                }
                if(view6 != null){
                    view6.setAlpha(0.4f);
                }
                if(view7 != null){
                    view7.setAlpha(0.2f);
                }
                break;
            case 5:
                break;
            case 6:
                break;
        }
//        if(getChildCount() >= 4) {
//            View view2 = getChildAt(2);
//            View view4 = getChildAt(4);
//            if (view2 != null) {
//                view2.setAlpha(0.6f);
//            }
//            if (view4 != null) {
//                view4.setAlpha(0.6f);
//            }
//            View view1 = getChildAt(1);
//            View view5 = getChildAt(5);
//            if (view1 != null) {
//                view1.setAlpha(0.4f);
//            }
//            if (view5 != null) {
//                view5.setAlpha(0.4f);
//            }
//            View view0 = getChildAt(0);
//            if (view0 != null) {
//                view0.setAlpha(0.2f);
//            }
//            View view6 = getChildAt(6);
//            if (view6 != null) {
//                view6.setAlpha(0.2f);
//            }
//        }

    }
    public void recycle(){

    }
}
